﻿/* 9. Write a program that finds the most frequent number in an array. 
 * Example: {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} -> 4 (5 times) */

using System;

public class MostFrequentNumbers
{
    public static void Main()
    {
        int n;
        int maxTimes = 0;
        int currentTimes;

        Console.Write("\nThe length of array: ");
        n = int.Parse(Console.ReadLine());

        if (0 < n)
        {
            string[] array = new string[n]; // the program will works and with string (without 'null') members of array

            Console.WriteLine();
            for (int i = 0; i < n; i++)
            {
                Console.Write("Array[{0}] = ", i + 1);
                array[i] = Console.ReadLine();
            }

            for (int i = 0; i < n; i++)
            {
                currentTimes = 1;
                for (int j = i + 1; j < n; j++)
                {
                    if ((array[i] != null) && (array[i] == array[j]))
                    {
                        currentTimes++;
                        array[j] = null;
                    }
                }

                if (maxTimes < currentTimes)
                {
                    maxTimes = currentTimes;
                    for (int j = 0; j < i; j++)
                        array[j] = null;
                }
                else if (currentTimes < maxTimes)
                    array[i] = null;
            }

            for (int i = 0; i < n; i++)
            {
                if (array[i] != null)
                {
                    Console.Write("\nThe most frequent numbers are {0} {1}", "{", array[i]);
                    for (int j = i + 1; j < n; j++)
                        if (array[j] != null)
                            Console.Write(", {0}", array[j]);

                    Console.WriteLine("{0}\n", " }");
                    break;
                }
            }
        }
        else
            Console.WriteLine("\nThe length of array must be natural!\n");
    }
}